package org.example.dictionary.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.dictionary.domain.Dictionary;
import org.example.dictionary.domain.DictionaryContent;
import org.example.dictionary.service.DictionaryContentService;
import org.example.dictionary.service.DictionaryService;
import org.example.dictionary.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("dictionary")
public class DictionaryController {
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private DictionaryContentService dictionaryContentService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("list")
    public Result  list() {
        if(!redisTemplate.hasKey("dictionaryList")){
            List<Dictionary> list = dictionaryService.list();
            redisTemplate.opsForList().rightPushAll("dictionaryList",list);
        }
        List list = redisTemplate.opsForList().range("dictionaryList", 0, -1);
        return Result.success(list);
    }

    @RequestMapping("getContent")
    public Result  getContent(Integer dictId) {

        if(!redisTemplate.hasKey("contentList"+dictId)){
            LambdaQueryWrapper<DictionaryContent> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DictionaryContent::getDictId,dictId);
            List<DictionaryContent> list = dictionaryContentService.list(wrapper);
            redisTemplate.opsForList().rightPushAll("contentList"+dictId,list);
        }
        List list = redisTemplate.opsForList().range("contentList"+dictId, 0, -1);
        return Result.success(list);

    }


}
